package com.dy.nygl.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.omg.Messaging.SyncScopeHelper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.dy.nygl.pojo.User;
import com.dy.nygl.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {
	private int id;
	private String username;
	private String password;
	private User user1;

	public User getUser1() {
		return user1;
	}

	public void setUser1(User user1) {
		this.user1 = user1;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Resource(name = "userService")
	private UserService userService;

	@RequestMapping("/changePasss.action")
	public ModelAndView changPasss(@Param("newPass") String newPass, HttpServletResponse response) throws IOException {
		User user2 = new User();
		user2.setId(getId());
		user2.setPassword(newPass);
		userService.updateUser(user2);
		ModelAndView model = new ModelAndView();
		model.addObject("name", getUsername());
		model.setViewName("success");
		return model;
	}

	@RequestMapping(value = "/changePassword.action")
	public ModelAndView changePassword() {
		ModelAndView model = new ModelAndView();
		model.setViewName("chang_password");
		return model;
	}

	@RequestMapping("/validate.action")
	public void validatePass(@Param("old") String old, HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		if (old.equals(getPassword())) {
			out.print(1);
		} else {
			out.print(2);
		}
	}

	@RequestMapping("/validate1.action")
	public void validate1(@Param("new1") String new1, HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		if (new1.equals(getPassword())) {
			out.print(1);
		} else {
			out.print(2);
		}
	}

	@RequestMapping("/setInfo.action")
	public void setInfo(@Param("id") Integer id, HttpServletResponse response) throws IOException {
		user1 = userService.selectOne(id);
		setId(user1.getId());
		setUsername(user1.getAccount());
		setPassword(user1.getPassword());
		setUser1(user1);
		PrintWriter out = response.getWriter();
		out.print(2);
	}

	@RequestMapping("/getUserInfo.action")
	public ModelAndView userInfo() {
		String name=getUsername();
		User user = userService.selectUserByName(name);
		ModelAndView model = new ModelAndView();
		model.addObject("userInfo", user);
		model.setViewName("UserInfo");
		return model;
	}

	@RequestMapping(value = "/checkUser.action", method = { RequestMethod.POST })
	public void checkUser(HttpServletRequest request, HttpServletResponse response, HttpSession session)
			throws ServletException, IOException {

		PrintWriter out = response.getWriter();
		String name = request.getParameter("name");
		if (name.trim().equals("")) {
			out.print(2);// 2�ǲ���Ϊ��
		} else {
			User user = userService.checkUser(name);
			if (user != null) {
				out.print(1);
			} else {
				out.print(3);
			}
		}
	}

	@RequestMapping(value = "/login.action", method = { RequestMethod.POST })
	public ModelAndView login(HttpServletRequest request, HttpServletResponse response, HttpSession session)
			throws IOException {
		String account = request.getParameter("username");
		String password = request.getParameter("password");
		ModelAndView model = new ModelAndView();
		User user = userService.login(account, password);
		session.setAttribute("user", user);
		if (user == null) {
			model.setViewName("error");
		} else {
			setUsername(account.trim());
			System.err.println("初始化Cookie");
			Cookie cookie1 = new Cookie("cong", null);
			cookie1.setMaxAge(0);
			// setCookie
			System.err.println("设置的Cookie参数："+account);
			Cookie cookie = new Cookie("cong", account.trim());
			cookie.setMaxAge(60000);
			response.addCookie(cookie);
			model.addObject("user", user);
			model.setViewName("ToIndex");
		}
		return model;
	}

	@RequestMapping("/loginOut.action")
	public void loginOut(HttpServletResponse response) throws IOException {
		System.err.println("销毁Cookie");
		Cookie cookie = new Cookie("cong", null);
		cookie.setMaxAge(0);
		PrintWriter out = response.getWriter();
		out.print(2);

	}

	@RequestMapping(value = "/getList.action")
	public ModelAndView getList(HttpSession session) throws IOException {
		List<User> userList = new ArrayList<>();
		ModelAndView model = new ModelAndView();
		userList = userService.selectAll();
		model.addObject("userList", userList);
		model.setViewName("UserInfo");
		return model;
	}

	/**
	 * �û���ȫ�˳�
	 */
	@RequestMapping("/logout.action")
	public ModelAndView logout(HttpSession session) throws IOException {
		session.invalidate();
		ModelAndView model = new ModelAndView();
		model.setViewName("login");
		return model;
	}

	/**
	 * �û����޸��û���Ϣ �˺����id�����޸����඼�ɶ� Ϊ�޸�ҳ����ʾ��������
	 * ������Ա���޸��û������Ҫͨ�������ݿ����
	 */
	@RequestMapping("/userUpdateDisplay.action")
	public ModelAndView userUpdateDisplay(@RequestParam("userUpdate") Integer id, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		User user = userService.selectOne(id);
		ModelAndView model = new ModelAndView();
		model.addObject("user", user);
		model.setViewName("userUpdate");
		return model;
	}

	/**
	 * �ύ��Ϣ��ת��login����
	 */
	@RequestMapping("/userUpdateSubmit.action")
	public ModelAndView userUpdateSubmit(@RequestParam("userSubmit") Integer id, HttpServletRequest request)
			throws IOException {
		request.setCharacterEncoding("utf-8");
		User user = new User();
		user.setId(id);
		user.setName(request.getParameter("name"));
		user.setAccount(request.getParameter("account"));
		user.setPassword(request.getParameter("password"));
		user.setIdentity(request.getParameter("identity"));
		user.setMailbox(request.getParameter("mailbox"));
		int i = userService.updateUser(user);
		ModelAndView model = new ModelAndView();
		if (i > 0) {
			model.setViewName("login");
		} else {
			model.setViewName("userUpdate");
		}
		return model;
	}

	/**
	 * ����Ա���������û� 1�����û� 2�������Ա
	 */
	@RequestMapping("/adminSelect.action")
	public ModelAndView adminSelect(@RequestParam("user") String identity, HttpSession session) throws IOException {
		List<User> userList = new ArrayList<>();
		ModelAndView model = new ModelAndView();
		if ("1".equals(identity) || "" == identity) {
			model.setViewName("user");
		} else if ("2".equals(identity)) {
			userList = userService.selectAll();
			model.addObject("userList", userList);
			model.setViewName("userlist");
		}
		return model;
	}

	/**
	 * ɾ���û� �ɹ��ص���ǰҳ�棬ʧ��error
	 */
	@RequestMapping("/adminDelete.action")
	public ModelAndView adminDelete(@RequestParam("id") Integer id) throws IOException {
		ModelAndView model = new ModelAndView();
		boolean s = userService.deleteUser(id);
		if (s == true) {
			model.setViewName("userlist");
		} else {
			model.setViewName("error");
		}
		return model;
	}

}
